package nero.yncc.com.createart.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import cn.finalteam.toolsfinal.StringUtils;
import nero.yncc.com.createart.R;
import nero.yncc.com.createart.activity.ActivityChangjingDetail;
import nero.yncc.com.createart.bean.ChangjingBean;
import nero.yncc.com.createart.custom.MyRecyclerView;
import nero.yncc.com.createart.utils.ImageUtils;
import nero.yncc.com.createart.utils.TextUtil;

/**
 * Created by nero on 17-6-21.
 */

public class AdapterIndexChangjing extends BaseAdapter {

    Context context;
    ArrayList<ChangjingBean> arrayList;

    public AdapterIndexChangjing(Context context, ArrayList<ChangjingBean> arrayList) {
        this.context = context;
        this.arrayList = arrayList;
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder;
        if (view == null) {
            view = LinearLayout.inflate(context, R.layout.adapter_index_changjing, null);
            holder = new Holder(view);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }

        final ChangjingBean bean = arrayList.get(i);
        //平凑所有场景字符串
        ArrayList<ChangjingBean.ChangjingInBean> arrayList = (ArrayList<ChangjingBean.ChangjingInBean>) bean.getCategory_list();
        if (arrayList == null) {
            arrayList = new ArrayList<>();
        }
        String allName = "";
        for (ChangjingBean.ChangjingInBean ChangjingBean : arrayList) {
            allName += ChangjingBean.getName() + "+";
        }
        if (allName.length() > 1)
            allName = allName.substring(0, allName.length() - 1);
        holder.tv_title.setText(TextUtil.getText(bean.getName(), "暂无名称"));
        holder.tv_content.setText(TextUtil.getText(bean.getFengge_name(), "") + "  " +
                TextUtil.getText(bean.getMianji() + "m²", "") + "  " +
                TextUtil.getText(bean.getHuxing_name(), "") + "  ");
        holder.tv_allname.setText(allName);
        holder.tv_title_right.setText("共" + arrayList.size() + "个场景");
        if (arrayList.size() > 0)
            ImageUtils.BindImage(context, holder.iv_image, arrayList.get(0).getPhoto(), 3);
        holder.tv_content_right2.setText("省￥" + TextUtil.getText(bean.getSheng(), "省￥0"));
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ActivityChangjingDetail.class);
                intent.putExtra("data", bean);
                context.startActivity(intent);
            }
        });
        return view;
    }

    class Holder extends RecyclerView.ViewHolder {
        public Holder(View itemView) {
            super(itemView);
            iv_image = itemView.findViewById(R.id.iv_image);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_content = itemView.findViewById(R.id.tv_content);
            tv_allname = itemView.findViewById(R.id.tv_allname);
            tv_title_right = itemView.findViewById(R.id.tv_title_right);
            tv_content_right2 = itemView.findViewById(R.id.tv_content_right2);
        }

        final ImageView iv_image;
        final TextView tv_title, tv_content, tv_allname, tv_title_right, tv_content_right2;
    }
}
